Repository navigation
perf(docx): stop reading a table's rows quadratically and stop XMLBeans throwing on every colour - #1084
Merged
Conversation
…ytes POI's XWPFTable.getRow and getNumberOfRows count the table's w:tr elements on every call, and writing a table read each row that way several times, so the reads grew with the square of its rows. The row passes of writeTableRows now read POI's own list of the table's rows. A Word colour attribute is a union of the word auto and three hex bytes. Given a hex string, XMLBeans tried auto first and threw and caught an XmlValueOutOfRangeException for every other colour, four for each ruled cell. Every RGB colour the export writes is now given as its bytes (DocxTranslucency.rgb), which XMLBeans writes as the same upper-case hex. The exact-line count and the closing-space fill asked each paragraph and run for a few children through an XPath each; DocxXmlChildren walks the children instead and selects the same elements in the same order. table1000 allocates 233 -> 158 MB per export. The DOCX corpus and the benchmark documents are byte-identical before and after.
…run order The CHANGELOG entry said table1000 was faster in each of four interleaved rounds; it was faster in three. Its allocation figures are now the mean of the two runs, 233 -> 159 MB per export for table1000, rounded alike on both sides, and its colour sentence names the attributes the change covers, w:color and w:fill, rather than every colour in the file. rowOf's javadoc said the row list holds the same rows as the XML for a table written through POI; getRow reads that same list itself, and only its index check counts the rows, which is the reason the change is safe. DocxClosingSpacesTest gains a paragraph of a run, a link, a simple field and a run, read back from the end in the order they stand.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
A thousand-row table took 657 ms and allocated 232–234 MB to export as DOCX (
DocxExportJmhBenchmark,table1000), where the PDF render of the same document takes about a tenth of the CPU time (62 msagainst 688 ms, measured below).
A profile of
DocxExportJmhBenchmark(table1000) put the cost in three places that are not thewriting of the table itself:
XWPFTable.getRow(int)andgetNumberOfRows()call
CTTbl.sizeOfTrArray()on every call, and that counts the table'sw:trelements. The exportreads each row that way several times while writing it (
writeTableRows,holdRowHeight,rulesBeyondOne,carryTheRowsRounding,breakRowsWhereTheLayoutDoes,carryDrawingsInRows), sothe reads grew with the square of the rows:
sizeOfTrArraywas 18.7% of the export's CPU samples.w:color,w:fill) is a union of theword
autoand three hex bytes. Given a string, XMLBeans triesautofirst and throws, and catches,an
XmlValueOutOfRangeException— stack trace and all — for every other colour; four times for eachruled cell. Of the export's allocation, 7.8% was the exceptions and 20% the string set that threw them.
spaces of shaded runs asked every paragraph and run for a few of its children with
selectPath.What changed
rowOf(table, index)reads a row from POI's own list of the table's rows (getRows()), andrulesBeyondOnetakes the count from it too. The loops that use it all run on the tablewriteTableRowshas just created through POI.getRowitself reads that list; what it adds isthe count of
w:trto check the index, and every index read here is in range. Row reads outsidewriteTableRowsare left alone: elsewhere they are onegetRow(0)a table, except inDocxTableSplit, which reads row by row too but runs only on a table cut where its pages'margins change or in a panel — off the path measured here.
DocxTranslucency.rgb(Color)gives a colour as its three bytes, and every RGB colour attributethe export writes now takes those (a chip's
w:color="auto"stays a string: it matches theunion's first member, and nothing is thrown): cell and paragraph borders (
paintEdge), cell and chip shading, arun's colour, the Normal style's colour and the timeline rail's border. XMLBeans writes the bytes as
upper-case hex, the text
hex(Color)wrote. A run's colour is set on itsw:colordirectly, asXWPFRun.setColordoes, since that method takes only a string.DocxXmlChildren.path(from, steps...)/first(...)walk an element's children by name and selectwhat an XPath of child steps selects, in document order, typed and untyped XML alike.
isWrittenExact,holdsTextOfItsOwnandDocxClosingSpaces(a paragraph's runs, a run's shading,a drawing's inline) use it. The whole-document
.//w:pand.//w:tselections stayselectPath:measured on the same document,
selectPath(".//w:p")took 12–13 ms against 17–20 ms for a cursorwalk returning the same 5,006 paragraphs, so those passes were not the cost.
No public API changes; PDF and PPTX are untouched.
Measurements
JMH
DocxExportJmhBenchmark.docx,-prof gc,gc.alloc.rate.normper export — the number loaddoes not move (two runs of both builds, JDK 24, base =
developat the branch point; 3 forks × 5iterations, then 4 interleaved forks × 5):
Time was measured on a machine other sessions' Gradle builds were loading (up to 99% CPU), so
wall-clock means alone do not compare. Two measurements that do:
Interleaved JMH, one fork of each build a round, alternating which goes first,
ms/opper round:table1000is faster in three rounds of four (the fourth, 788 → 914, the other way); thesmaller shapes move within the noise.
Thread CPU time per
table1000export (median of 20 after 8 warm-up, three rounds, alternating):1313 → 797, 984 → 891, 688 → 500 ms. In the last round the machine was quiet (wall ≈ CPU:
697 → 544 ms). The PDF render of the same document: 62 ms CPU.
On a quiet machine before the change, JMH gave
table1000657 ± 250 ms/op.Output unchanged
byte-identical, and the export report's 1,718 notes the same.
docProps/core.xml, which holds the export's creation time.Tests
DocxXmlChildrenTest(new): a path selects the same objects as its XPath, in the same order, overruns and links interleaved, and over untyped XML;
firstis the first in document order andnullwhere the path ends in nothing.
DocxTranslucencyTest.aColourGivenItsBytesIsWrittenAsItsHexIs:w:coloron a border,w:fillona shading and
w:valon a run colour write the same XML from the bytes as from the hex, for fivecolours (zero and full bytes, a leading-zero byte, a translucent colour).
DocxTranslucencyTest.anOpaqueBodyColourIsTheNormalStylesColour: the Normal style's colour, whichno test read other than as black.
DocxClosingSpacesTest.aLinksAndAFieldsRunsAreReadInTheirPlacesAmongThePlainRuns: a plain run, alink, a simple field and a plain run, in that order, are read back from the end in the order they
stand — the walk the hand-written run list replaces an XPath union for.
DocxExportReportTest.aParagraphWhoseTextIsAllInALinkIsCountedAmongTheExactOnes: the link branchof
holdsTextOfItsOwn; the test asserts the fixture's paragraph has its text only insidew:hyperlink.(rows read from the first row, the last row never last, byte order, each colour site, a step taking
only its first child, links and simple fields left out of a paragraph's runs or read out of their
places, the shading and the
spacing read off the wrong element, an inline drawing passed over, the run and link branches of the
text check).
./mvnw -B -ntp clean install -pl :graph-compose-render-docx -am: core 845, render-pdf 347,render-docx 2787 tests, 0 failures.
./mvnw -B -ntp test -f qa/pom.xml: 1926 tests, 0 failures.